package com.polaris.live.ui.party.widget

import android.content.Context
import android.text.Editable
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.polaris.live.databinding.WidgetPasswordViewBinding
import com.polaris.live.interface_bag.TextWatcherAdapter

/**
 *
 * @author Created by 天晴 on 2024/3/11 13:40
 * @since 1.0.0
 **/
class PasswordView(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {
    val mBinding = WidgetPasswordViewBinding.inflate(LayoutInflater.from(context), this)

    init {
        initListener()
    }

    fun requestOneFocus() {
        mBinding.etNumberOne.requestFocus()
    }

    fun requestFourFocus() {
        mBinding.etNumberFour.requestFocus()
    }

    fun clearAllFocus() {
        mBinding.etNumberOne.clearFocus()
        mBinding.etNumberTwo.clearFocus()
        mBinding.etNumberThree.clearFocus()
        mBinding.etNumberFour.clearFocus()
    }

    fun setText(password: String) {
        if (password.length == 4) {
            mBinding.etNumberOne.text =
                Editable.Factory.getInstance().newEditable(password.substring(0, 1))
            mBinding.etNumberTwo.text =
                Editable.Factory.getInstance().newEditable(password.substring(1, 2))
            mBinding.etNumberThree.text =
                Editable.Factory.getInstance().newEditable(password.substring(2, 3))
            mBinding.etNumberFour.text =
                Editable.Factory.getInstance().newEditable(password.substring(3, 4))
        }
        requestFourFocus()
    }

    fun clearText() {
        mBinding.apply {
            etNumberOne.text =
                Editable.Factory.getInstance().newEditable("")
            etNumberTwo.text =
                Editable.Factory.getInstance().newEditable("")
            etNumberThree.text =
                Editable.Factory.getInstance().newEditable("")
            etNumberFour.text =
                Editable.Factory.getInstance().newEditable("")
        }

    }

    fun getText(): String? {
        val one = mBinding.etNumberOne.text
        val two = mBinding.etNumberTwo.text
        val three = mBinding.etNumberThree.text
        val four = mBinding.etNumberFour.text
        return if (one != null && one.length == 1
            && two != null && two.length == 1
            && three != null && three.length == 1
            && four != null && four.length == 1
        ) {
            "$one$two$three$four"
        } else {
            null
        }
    }

    private fun initListener() {
        mBinding.apply {
            etNumberOne.addTextChangedListener(object : TextWatcherAdapter() {
                override fun afterTextChanged(s: Editable?) {
                    val one = etNumberOne.text
                    if (one != null) {
                        if (one.length == 1) {
                            etNumberTwo.requestFocus()
                        }
                    }

                }
            })
            etNumberTwo.addTextChangedListener(object : TextWatcherAdapter() {
                override fun afterTextChanged(s: Editable?) {
                    val two = etNumberTwo.text
                    if (two != null) {
                        if (two.length == 1) {
                            etNumberThree.requestFocus()
                        }
                    }
                }
            })
            etNumberThree.addTextChangedListener(object : TextWatcherAdapter() {
                override fun afterTextChanged(s: Editable?) {
                    val three = etNumberThree.text
                    if (three != null) {
                        if (three.length == 1) {
                            etNumberFour.requestFocus()
                        }
                    }
                }
            })
        }
    }

    override fun dispatchKeyEvent(event: KeyEvent): Boolean {
        if (event.keyCode == KeyEvent.KEYCODE_DEL && event.action == KeyEvent.ACTION_DOWN) {
            mBinding.apply {
                if (etNumberOne.hasFocus()) {
                    val one = etNumberOne.text
                    if (one != null) {
                        if (one.length == 1) {
                            etNumberOne.text = Editable.Factory.getInstance().newEditable("")
                        }
                    }
                }
                if (etNumberTwo.hasFocus()) {
                    val one = etNumberTwo.text
                    if (one != null) {
                        if (one.length == 1) {
                            etNumberTwo.text = Editable.Factory.getInstance().newEditable("")
                        }
                        etNumberOne.requestFocus()
                    }
                }
                if (etNumberThree.hasFocus()) {
                    val one = etNumberThree.text
                    if (one != null) {
                        if (one.length == 1) {
                            etNumberThree.text = Editable.Factory.getInstance().newEditable("")
                        }
                        etNumberTwo.requestFocus()
                    }
                }
                if (etNumberFour.hasFocus()) {
                    val one = etNumberFour.text
                    if (one != null) {
                        if (one.length == 1) {
                            etNumberFour.text = Editable.Factory.getInstance().newEditable("")
                        }
                        etNumberThree.requestFocus()
                    }
                }
            }
            return true
        }
        return super.dispatchKeyEvent(event)
    }
}